// pages/progressqryform/progressqryform.js
import { Target, HttpUtil } from '../../../utils/http_util.js'
var config = require('../../../utils/config.js')
var utils = require('../../../utils/util.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    //表单对象
    formData: {
      userType: '0',
      phone: '',
      signCode:'',
    },

    errormsg:{
      userNameEM:'',
      phoneEM: '',
      signCodeEM: '',
    }

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  },
  //登录类型变更
  onSelectChange(event) {
    var that = this;
    that.data.formData.userType = event.detail;
    that.setData({
      formData: that.data.formData
    });
  },
  /**
   * 取消
   * 
   */
  cancel() {
    wx.navigateBack({
      delta: 1
    });
  },
  /**
   * 点击确定按钮
   * 
   */
  submit() {
    //提交
    var that = this;
    var data = that.data.formData;
    var errormsg = that.data.errormsg;
    var isPass= true;

    // var uPattern = /^[\u4E00-\u9FA5A-Za-z0-9]+$/;
    // if (!uPattern.test(data.userName)){
    //   wx.showToast({
    //     title: '用户名格式不正确',
    //     icon: 'none',
    //     duration: 2000
    //   })
    //   //errormsg.userNameEM = '用户名格式不正确';
    //   isPass =false;
    // }
    var uPhone = /^1(3|4|5|7|8)\d{9}$/;
    if (!uPhone.test(data.phone)) {
      wx.showToast({
        title: '用机号码不正确',
        icon: 'none',
        duration: 2000
      })
      //errormsg.phoneEM = '用机号码不正确';
      
      isPass = false;
    }
    var ucode = /^\d{4}$/;
    if (!ucode.test(data.signCode)) {
      wx.showToast({
        title: '验证码未填写或格式不正确',
        icon: 'none',
        duration: 2000
      })
      //errormsg.phoneEM = '用机号码不正确';

      isPass = false; 
    }
    if(!isPass){
      // that.setData({
      //   errormsg: errormsg
      // })
      return false;
    }else{ //校验成功登陆
      HttpUtil.get(Target.self, "wx/small/login", { phone: data.phone, code: data.signCode, userType: data.userType}).then(res => {
        if(res.status=='0000'){
          wx.showToast({
            title: '登录成功！',
            icon: 'none',
            duration: 2000
          })
          wx.setStorageSync('userInfo', res.data)
          wx.setStorageSync('userType', res.userType)
          wx.reLaunch({
            url: '/pages/user/user-main/main'
          })

        }else{
          wx.showToast({
            title: res.msg,
            icon: 'none',
            duration: 2000
          })
        }
      })
    }   
  },
  /**
   * 绑定输入框 
   * 
  */
  onFieldChange(event) {
    // var that =this;
    // var em = this.data.errormsg;
    this.data.formData[event.currentTarget.id] = event.detail;
    // em[event.currentTarget.id + 'EM'] = '';
    // that,setData({
    //   errormsg:em
    // })
  },
  sendmeg: function (event){
    //提交
    var that = this;
    var data = that.data.formData;
    var errormsg = that.data.errormsg;
    var isPass = true;

    var uPhone = /^1(3|4|5|7|8)\d{9}$/;
    if (!uPhone.test(data.phone)) {
      wx.showToast({
        title: '用机号码不正确',
        icon: 'none',
        duration: 2000
      })
      //errormsg.phoneEM = '用机号码不正确';

      isPass = false;
    }
    if (!isPass) {
      // that.setData({
      //   errormsg: errormsg
      // })
      return false;
    }else{
      that.setData({
        laodfalg:true
      })
      HttpUtil.get(Target.self, "wx/small/getSmCode", { phone: data.phone}).then(res => {
        that.setData({
          laodfalg: false
        })
        console.log(res);
        if(res.status!='0000'){
          wx.showToast({
            title: '短信发送异常！',
            duration: 2000,
            icon: "none"
          })  
        }else{
          wx.showToast({
            title: '短信发送成功！',
            duration: 2000,
            icon: "none"
          })  
        }
      });
    }
  }
})